﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Runtime.InteropServices;
using System.Collections.Specialized;
using System.Text;
using System.IO;
namespace FTchina.Library
{
    /// <summary>
    ///IniHelper 的摘要说明
    /// </summary>
    public class IniHelper
    {
        string FileName = string.Empty;
        public IniHelper()
        {
            FileName =FileHelper.GetFullPath("~/datax.ini");
            if (!FileHelper.IsExistFile(FileName))
            {
                FileHelper.CreateFile(FileName);

            }
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        [DllImport("kernel32")]
        private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);

        public int UpdateINIFile(string SectionName, string Key, string value)
        {
            if (!WritePrivateProfileString(SectionName, Key, value, FileName))
            {
                return -1;
            }
            return 0;
        }
        public string ReadString(string section, string key, string Default)
        {
            Byte[] byBuffer = new Byte[65535];
            int nBufLen = GetPrivateProfileString(section, key, Default, byBuffer, byBuffer.GetUpperBound(0), FileName);

            string s = Encoding.GetEncoding(0).GetString(byBuffer);
            s = s.Substring(0, nBufLen);
            return s.Trim();
        }


        //public int ReadSections(StringCollection SectionList, string filePath)
        //{
        //    Byte[] byBuffer = new Byte[65535];

        //    // 通过GetPrivateProfileString（）方法读取的结果存放在byBuffer中，以整数值的形式存在。
        //    int nBufLen = GetPrivateProfileString(null, null, null, byBuffer, 65535, filePath);
        //    if (nBufLen == 0)
        //    {
        //        return -1;
        //    }

        //    SearchStringsFromBuffer(byBuffer, nBufLen, SectionList);

        //    return 0;
        //}


        //public int ReadSectionKeys(string section, StringCollection keys, string filePath)
        //{
        //    Byte[] byBuffer = new Byte[16384];

        //    int nBufLen = GetPrivateProfileString(section, null, null, byBuffer, byBuffer.GetUpperBound(0),
        //     filePath);
        //    if (nBufLen == 0)
        //    {
        //        return -1;
        //    }

        //    SearchStringsFromBuffer(byBuffer, nBufLen, keys);

        //    return 0;
        //}

        //private void SearchStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings)
        //{
        //    Strings.Clear();

        //    int start = 0;
        //    for (int i = 0; i < bufLen; i++)
        //    {
        //        if ((Buffer[i] == 0) && ((i - start) > 0))
        //        {
        //            String s = Encoding.GetEncoding(0).GetString(Buffer, start, i - start);
        //            Strings.Add(s);
        //            start = i + 1;
        //        }
        //    }
        //}

        //public int DeleteSectionbySectionName(string Section, string filePath)
        //{
        //    if (!WritePrivateProfileString(Section, null, null, filePath))
        //    {
        //        return -1;
        //    }
        //    return 0;
        //}
        //public int DeleteKeybySectionName(string Section, string Key, string filePath)
        //{
        //    if (!WritePrivateProfileString(Section, Key, null, filePath))
        //    {
        //        return -1;
        //    }
        //    return 0;
        //}


    }
}